package com.bocai.yuweiwangluo.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.View.XListView;
import com.bocai.yuweiwangluo.base.BaseActivity;
import com.bocai.yuweiwangluo.bean.CityAndTypeBean;
import com.bocai.yuweiwangluo.bean.MatchListBean;
import com.bocai.yuweiwangluo.bean.PopBean;
import com.bocai.yuweiwangluo.presenter.MatchListPresenter;
import com.bocai.yuweiwangluo.utils.PopUtil;
import com.bocai.yuweiwangluo.viewModel.IMatchListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

/**
 * 约战界面
 */
public class MatchActivity extends BaseActivity implements View.OnClickListener,AdapterView.OnItemClickListener,IMatchListView,XListView.IXListViewListener
{

    @ViewInject(R.id.lv_match_list)
    XListView lv_match_list;
    @ViewInject(R.id.ll_match_sporttype)
    LinearLayout ll_match_sporttype;
    @ViewInject(R.id.ll_match_area)
    LinearLayout ll_match_area;
    @ViewInject(R.id.tv_match_sporttype)
    TextView tv_match_sporttype;
    @ViewInject(R.id.tv_match_area)
    TextView tv_match_area;
    @ViewInject(R.id.iv_match_sporttype)
    ImageView iv_match_sporttype;
    @ViewInject(R.id.iv_match_area)
    ImageView iv_match_area;
    @ViewInject(R.id.ll_nocotent)
    LinearLayout llnoContent;
    @ViewInject(R.id.ll_refresh)
    LinearLayout llRefresh;
    private int width;
    private PopUtil pu;
    private ArrayList<PopBean> list;
    private MatchListPresenter matchListPresenter;
    private CityAndTypeBean cityAndTypeBean;
    private String type_id="0";
    private String city_id="0";
    private int page=1;
    @Override
    protected int contentView()
    {
        return R.layout.activity_match;
    }

    @Override
    protected void setData()
    {
        ViewUtils.inject(this);
        initToolbar("约战");
        mToolbar.inflateMenu(R.menu.menu_talk);
        WindowManager windowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
        width=windowManager.getDefaultDisplay().getWidth() / 2;
        getMatchListPresenter();
        matchListPresenter.getMatchList("1", "0", "0");
        initEvent();
    }

    @Override
    protected void initEvent()
    {
        super.initEvent();
        llRefresh.setOnClickListener(this);
        lv_match_list.setOnItemClickListener(this);
        ll_match_sporttype.setOnClickListener(this);
        ll_match_area.setOnClickListener(this);
        lv_match_list.setPullLoadEnable(true);
        lv_match_list.setPullRefreshEnable(true);
        lv_match_list.setXListViewListener(this);
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener()
        {
            @Override
            public boolean onMenuItemClick(MenuItem item)
            {
                startActivity(new Intent(MatchActivity.this, ReleaseMatchActivity.class));
                return true;
            }
        });
    }


    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ll_refresh:
                llRefresh.setVisibility(View.GONE);
                onRefresh();
                break;
            case R.id.ib_title_right:
                startActivity(new Intent(this,ReleaseMatchActivity.class));
                break;
            case R.id.ll_match_sporttype:
                cityAndTypeBean= matchListPresenter.getCityAndTypeBean();
                if(cityAndTypeBean.getType()!=null)
                {
                    showPopwindow(v,tv_match_sporttype,setTypeList(),0);
                }
                break;
            case R.id.ll_match_area:
                cityAndTypeBean= matchListPresenter.getCityAndTypeBean();
                if(cityAndTypeBean.getCity()!=null)
                {
                    showPopwindow(v,tv_match_area,setCityList(),1);
                }
                break;
        }
    }
    private ArrayList<PopBean> setCityList()
    {
        try{
            list=new ArrayList<>();
            cityAndTypeBean= matchListPresenter.getCityAndTypeBean();
            if(cityAndTypeBean!=null)
            {

                for (int i=0;i<cityAndTypeBean.getCity().size()+1;i++)
                {
                    PopBean popBean=new PopBean();
                    if(i==0)
                    {
                        popBean.setId("0");
                        popBean.setName("全部地区");
                    }else
                    {
                        popBean.setId(cityAndTypeBean.getCity().get(i-1).getId());
                        popBean.setName(cityAndTypeBean.getCity().get(i-1).getName());
                    }
                    list.add(i,popBean);
                }
            }
        }catch (Exception e){}
        return list;

    }
    private ArrayList<PopBean> setTypeList()
    {
        try{
            list=new ArrayList<>();
            cityAndTypeBean= matchListPresenter.getCityAndTypeBean();
            if(cityAndTypeBean!= null)
            {
                for (int i=0;i<cityAndTypeBean.getType().size()+1;i++)
                {
                    PopBean popBean=new PopBean();
                    if(0==i)
                    {
                        popBean.setId("0");
                        popBean.setName("全部运动");

                    }else
                    {
                        popBean.setId(cityAndTypeBean.getType().get(i-1).getId());
                        popBean.setName(cityAndTypeBean.getType().get(i-1).getTitle());
                    }

                    list.add(i,popBean);
                }
            }
        }catch (Exception e)
        {

        }
        return list;
    }
    private void showPopwindow(View v,final TextView tv,ArrayList<PopBean> list,final int what)
    {
    try{

        pu=PopUtil.getInstance(this,v,width,width,list);
        pu.setListener(
                new PopUtil.OnDatePopClickListener()
                {
                    @Override
                    public void onClick(PopUtil pu, PopBean date)
                    {
                        try
                        {
                            tv.setText(date.getName());
                            pu.dismiss();
                            if(0==what)
                            {
                                type_id=date.getId();
                                page=1;
                                matchListPresenter.getMatchList(page + "", type_id, city_id);
                            }else if(1==what)
                            {
                                city_id=date.getId();
                                page=1;
                                matchListPresenter.getMatchList(page + "", type_id, city_id);
                            }
                        }catch (Exception e)
                        {

                        }

                    }
                });
        pu.show();
    }catch (Exception e)
    {

    }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        try
        {
            MatchListBean.Data data= (MatchListBean.Data) parent.getAdapter().getItem(position);
            Intent intent=new Intent(this, MatchDetailActivity.class);
            intent.putExtra("id",data.getId());
            intent.putExtra("content",data.getContent());
            startActivity(intent);
        }catch (Exception e)
        {

        }
    }

    @Override
    public int getmyPage()
    {
        return page;
    }

    @Override
    public void load()
    {
        lv_match_list.stopRefresh();
        lv_match_list.stopLoadMore();
        lv_match_list.setRefreshTime("");
    }

    @Override
    public XListView getListView()
    {
        return lv_match_list;
    }

    @Override
    public LinearLayout getnoContent()
    {
        return llnoContent;
    }

    @Override
    public MatchListPresenter getMatchListPresenter()
    {
        matchListPresenter =new MatchListPresenter(this,this,this);
        return matchListPresenter;
    }

    @Override
    public LinearLayout getllRefresh()
    {
        return llRefresh;
    }

    @Override
    public void finishActivity()
    {
        this.finish();
    }

    @Override
    public void onRefresh()
    {
        page=1;
        lv_match_list.setPullLoadEnable(true);
        matchListPresenter.getMatchList(page + "", type_id, city_id);
    }

    @Override
    public void onLoadMore()
    {
        page++;
        matchListPresenter.getMatchList(page + "", type_id, city_id);
    }
}
